<template>
  <div class="mod-config">
    <div v-if="member">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
        <el-form-item label="请选择表单：">
          <el-select v-model="dataForm.formId" @change="search()" placeholder="请选择">
            <el-option
              v-for="item in formList"
              :key="item.id"
              :label="item.surveyName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <div style="width: 100%;border-bottom: 1px solid #efefef;margin-bottom: 20px;"></div>
        <el-form-item>
          <el-input v-model="dataForm.realName" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;"
      >
        <el-table-column prop="nickname" header-align="center" align="center" label="昵称"></el-table-column>
        <el-table-column prop="unionid" header-align="center" align="center" label="UnionID"></el-table-column>
        <el-table-column prop="channelName" header-align="center" align="center" label="渠道"></el-table-column>
        <el-table-column header-align="center" align="center" label="提交表单时间">
          <template slot-scope="scope">
            {{ formTime(scope.row.created_at) }}
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="状态">
          <template slot-scope="scope">
            {{ scope.row.memberStatus === 2?'未注册':'' }}
            {{ scope.row.memberStatus === 1?'会员':'' }}
            {{ scope.row.memberStatus === 0?'非会员':'' }}
          </template>
        </el-table-column>
        <el-table-column v-for="(item, index) in formHeader" :key="index" :prop="Object.keys(item)[0]" header-align="center" align="center" :label="Object.values(item)[0].label"></el-table-column>
        <el-table-column header-align="center" align="center" label="地址">
          <template slot-scope="scope">
            {{ scope.row.field_12.province+'-'+scope.row.field_12.city+'-'+scope.row.field_12.district }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addChannel(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>

      <!--添加渠道s-->
      <el-dialog
        :close-on-click-modal="false"
        :visible.sync="visible1"
        title="查看"
      >
        <el-row :gutter="20">
          <el-col :span="6" class="text_r">昵称</el-col>
          <el-col :span="18">：{{ formOne.nickname || '无'}}</el-col>
          <el-col :span="6" class="text_r">UnionId</el-col>
          <el-col :span="18">：{{ formOne.unionid || '无'}}</el-col>
          <el-col :span="6" class="text_r">渠道</el-col>
          <el-col :span="18">：{{ formOne.channelName || '无'}}</el-col>
          <el-col :span="6" class="text_r">状态</el-col>
          <el-col :span="18" v-if="formOne.memberStatus === 2">：未注册</el-col>
          <el-col :span="18" v-if="formOne.memberStatus === 1">：会员</el-col>
          <el-col :span="18" v-if="formOne.memberStatus === 0">：非会员</el-col>
        </el-row>
        <el-row :gutter="20" v-for="(item, index) in formHeader" :key="index">
          <el-col :span="6" class="text_r">{{ Object.values(item)[0].label }}</el-col>
          <el-col :span="18">：{{ formOne[Object.keys(item)[0]] || '无'}}</el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="visible1 = false">关闭</el-button>
        </span>
      </el-dialog>
      <!--添加渠道e-->

    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        member: true,
        isAdd: true,
        visibleIntegralList: false,
        dataForm: {

        },
        formHeader: [],
        dataForm1: {
          channelType: '',
          channelName: '',
          channelRemark: ''
        },
        rules: {
          channelName: [
            { required: true, message: '请输入渠道名称', trigger: 'blur' }
          ],
          channelType: [
            { required: true, message: '请选择渠道分类', trigger: 'blur' }
          ]
        },
        zanConfig: 0,
        visible1: false,
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        useroption: [
          {
            value: null,
            label: '全部'
          },
          {
            value: 1,
            label: '华北'
          },
          {
            value: 2,
            label: '东北'
          },
          {
            value: 3,
            label: '华东'
          },
          {
            value: 4,
            label: '中南'
          },
          {
            value: 5,
            label: '西部'
          }
        ],
        useroption1: [
          {
            value: null,
            label: '全部'
          },
          {
            value: 1,
            label: '未注册'
          },
          {
            value: 2,
            label: '非会员'
          },
          {
            value: 3,
            label: '会员'
          }
        ],
        formList: [],
        formOne: {},
        table: [
          {
            label:'姓名'
          },
          {
            label:'昵称'
          }
        ]
      }
    },
    activated() {
      this.getForm()
      this.getDataList()
    },
    methods: {
      search() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.dataForm.page = this.pageIndex
        this.dataForm.limit = this.pageSize
        if(!this.dataForm.realName){
          delete this.dataForm.realName
        }
        if(!this.dataForm.phone){
          delete this.dataForm.phone
        }
        this.$http({
          url: this.$http.adornUrl('/market/marketsurvey/list'),
          method: 'get',
          params: this.$http.adornParams(this.dataForm)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$refs['dataForm'].resetFields()
            this.totalPage = data.MarketSurveyInfoVo.total
            this.formHeader = JSON.parse(data.MarketSurveyInfoVo.formHeader).fields
            this.dataList = []
            data.MarketSurveyInfoVo.formList.map((item)=> {
              let newObj = item
              Object.entries(JSON.parse(item.formData)).map((itemChild) => {
                newObj[itemChild[0]] = itemChild[1]
              })
              this.dataList.push(newObj)
            })
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      formTime(time) {
        return time?new Date(+new Date(time)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,''):''
      },
      getForm() {
        this.$http({
          url: this.$http.adornUrl('/market/marketsurvey/getAllForm'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.formList = data.list
          }
        })
      },
      addChannel(index, channelInfo) {
        console.log(index)
        this.formOne = channelInfo
        this.visible1 = true
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      }
    }
  }
</script>
<style>
  .margin_10{
    margin-bottom: 10px;
  }
  .text_r{
    text-align: right;
  }
  .inineBlock{
    display: inline-block;
  }
  img{
    width: 100%;
    height: auto;
  }
</style>
